{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import chipwhisperer as cw\n",
    "\n",
    "try:\n",
    "    if not scope.connectStatus:\n",
    "        scope.con()\n",
    "except NameError:\n",
    "    scope = cw.scope()\n",
    "\n",
    "try:\n",
    "    if SS_VER == \"SS_VER_2_1\":\n",
    "        target_type = cw.targets.SimpleSerial2\n",
    "    elif SS_VER == \"SS_VER_2_0\":\n",
    "        raise OSError(\"SS_VER_2_0 is deprecated. Use SS_VER_2_1\")\n",
    "    else:\n",
    "        target_type = cw.targets.SimpleSerial\n",
    "except:\n",
    "    SS_VER=\"SS_VER_1_1\"\n",
    "    target_type = cw.targets.SimpleSerial\n",
    "\n",
    "try:\n",
    "    target = cw.target(scope, target_type)\n",
    "except:\n",
    "    print(\"INFO: Caught exception on reconnecting to target - attempting to reconnect to scope first.\")\n",
    "    print(\"INFO: This is a work-around when USB has died without Python knowing. Ignore errors above this line.\")\n",
    "    scope = cw.scope()\n",
    "    target = cw.target(scope, target_type)\n",
    "\n",
    "\n",
    "print(\"INFO: Found ChipWhisperer😍\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "scope.gain.gain = 55\n",
    "scope.gain.mode = 'high'\n",
    "scope.adc.samples = 5000\n",
    "scope.adc.offset = 0\n",
    "scope.adc.basic_mode = \"rising_edge\"\n",
    "scope.clock.clkgen_freq = 7372800\n",
    "scope.clock.adc_src = \"clkgen_x4\"\n",
    "scope.clock.adc_phase = 128\n",
    "scope.trigger.triggers = \"tio4\"\n",
    "scope.io.tio1 = \"serial_rx\"\n",
    "scope.io.tio2 = \"serial_tx\"\n",
    "scope.io.tio3 = True #Run condition\n",
    "scope.io.pdic = False #SPI forwarding off\n",
    "scope.io.hs2 = \"clkgen\"\n",
    "\n",
    "target.baud = 38400\n",
    "\n",
    "print(\"\\n***ESP 32 Target Usage Notes***\")\n",
    "print(\"   Call set_bootloader to enter BOOTLOADER so external USB-Serial cable will work.\")\n",
    "print(\"   Call set_run to enter RUN mode.\")\n",
    "print(\"   Jumper TDI to GND to disable boot-up messages (reduce noise if looking at boot decryption).\")\n",
    "\n",
    "\n",
    "def set_bootloader():\n",
    "    scope.io.tio3 = False\n",
    "    scope.io.tio1 = None\n",
    "    scope.io.tio2 = None\n",
    " \n",
    "    #Toggle reset\n",
    "    scope.io.nrst = False\n",
    "    time.sleep(0.05)\n",
    "    scope.io.nrst = None #Pulls high by default\n",
    "\n",
    "def set_run():\n",
    "    scope.io.tio3 = True\n",
    "    scope.io.tio1 = \"serial_rx\"\n",
    "    scope.io.tio2 = \"serial_tx\"\n",
    "    \n",
    "    #Toggle reset\n",
    "    scope.io.nrst = False\n",
    "    time.sleep(0.05)\n",
    "    scope.io.nrst = None #Pulls high by default"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
